﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ObjectModel.PowerPoint.New
{
    [XmlInclude(typeof(LinkedTextRange))]
    public abstract class LinkedObject
    {
        SourceObject mSourceObject;

        public SourceObject Source
        {
            get { return mSourceObject; }

            set
            {
                UnhookEvents();

                mSourceObject = value;
                if (mSourceObject is ObservableSourceObject)
                    ((ObservableSourceObject)mSourceObject).ValueChanged += SourceObject_ValueChanged;
            }
        }

        void SourceObject_ValueChanged(object sender, EventArgs e)
        {
            UpdateValue();
        }

        public abstract void UpdateValue();

        public void UnhookEvents()
        {
            if (mSourceObject is ObservableSourceObject && mSourceObject != null)
            {
                ((ObservableSourceObject)mSourceObject).ValueChanged -= SourceObject_ValueChanged;
                ((ObservableSourceObject)mSourceObject).UnhookEvents();
            }
        }
    }
}
